<?php
// +----------------------------------------------------------------------
// | Description: 产品
// +----------------------------------------------------------------------
// | Author:  Michael_xu | gengxiaoxu@5kcrm.com
// +----------------------------------------------------------------------
namespace app\crm\model;

use think\Db;
use app\admin\model\Common;
use think\Request;
use think\Validate;

class ConfirmationCategory extends Common
{
	/**
     * 为了数据库的整洁，同时又不影响Model和Controller的名称
     * 我们约定每个模块的数据表都加上相同的前缀，比如CRM模块用crm作为数据表前缀
     */
	protected $name = 'crm_confirmation_category';
    protected $createTime = '';
    protected $updateTime = '';
	protected $autoWriteTimestamp = true;

	/**
     * [getDataList 确认函类目list]
     * @author 
     * @param     [string]                   $map [查询条件]
     * @param     [number]                   $page     [当前页数]
     * @param     [number]                   $limit    [每页数量]
     * @return    [array]                    [description]
     */		
	public function getDataList($request)
    {
        $search = $request['search'];
        $where['title'] = ['like', '%'.$search.'%'];
        $list=$this->where($where)
            ->limit(($request['page']-1)*$request['limit'], $request['limit'])
            ->select();
        $dataCount=$this->where($where)->count();
        $data = [];
        $data['list'] = $list;
        $data['dataCount'] = $dataCount ? : 0;
        return $data;
    }
    /**
     * 创建确认函类目
     * @author
     * @param
     * @return
     */
    public function createData($param)
    {
        if(!empty($param["config"])){
            $param["config"]=json_encode($param["config"]);
        }
        $weight=0;
        $desc_data=$this->where(["confirmation_id"=>$param["confirmation_id"]])->order("weight","desc")->find();
        if(!empty($desc_data)) $weight=$desc_data["weight"]+1;
        $param["weight"]=$weight;
        if ($this->data($param)->allowField(true)->isUpdate(false)->save()) {
            //修改记录
            //updateActionLog($param['create_user_id'], 'crm_confirmation', $this->customer_id, '', '', '创建了确认函类目');
            $data = [];
            $data['title'] = $param['title'];
            return $data;
        } else {
            $this->error = '添加失败';
            return false;
        }
    }
    /**
     * 确认函类目详情
     * @param  $id 确认函类目ID
     * @return
     */
    public function getDataById($confirmation_category_id= '')
    {
        $map['confirmation_category_id'] = $confirmation_category_id;
        $dataInfo = $this->where($map)->find();
        if (!$dataInfo) {
            $this->error = '数据不存在或已删除';
            return false;
        }
        return $dataInfo;
    }
    /**
     * 删除确认函类目
     * */
    public function del($confirmation_category_id)
    {
        $map['confirmation_category_id'] = $confirmation_category_id;
        $dataInfo = $this->where($map)->find();
        if (!$dataInfo) {
            $this->error = '数据不存在或已删除';
            return false;
        }
        $dataInfo->delete();
        return true;
    }

    /**
     * 编辑确认函类目
     * @author Michael_xu
     * @param
     * @return
     */
    public function updateDataById($param, $confirmation_id = '')
    {
        $dataInfo = $this->get($confirmation_id);
        if (!$dataInfo) {
            $this->error = '数据不存在或已删除';
            return false;
        }
        if(!empty($param["config"])){
            $param["config"]=json_encode($param["config"]);
        }
        if ($this->allowField(true)->save($param, ['confirmation_id' => $confirmation_id])) {
            //修改记录
            //updateActionLog($user_id, 'crm_customer', $confirmation_id, $dataInfo->data, $param);
            $data = [];
            $data['confirmation_id'] = $confirmation_id;
            return $data;
        } else {
            $this->error = '编辑失败';
            return false;
        }
    }
    /**
     * 根据确认函id获取配置列表
     * */
    public function getListById($confirmation_id)
    {
        $list=$this->where(["confirmation_id"=>$confirmation_id])
            ->order("weight","asc")
            ->select();
        if(!empty($list)){
            foreach ($list as &$v){
                //类型:0=单行文本,1=多行文本,2=单项选择,3=多项选择,4=文本列表,5=时间,6=时间段
                $v["config"]=json_decode($v["config"],true);
            }
        }
        return $list;
    }
    /**
     * 排序
     * */
    public function setOrderBy($param)
    {
        $dataInfo = $this->get($param["confirmation_category_id"]);
        if (!$dataInfo) {
            $this->error = '数据不存在或已删除';
            return false;
        }
        $data=$this;
        $weight=0;
        if(empty($param["type"])){
            //上移
            $where["weight"]=["<",$dataInfo["weight"]];
            $data=$data->where($where)->order("weight","desc");
        }else{
            $where["weight"]=[">",$dataInfo["weight"]];
            $data=$data->where($where)->order("weight","asc");
        }
        $data=$data->find();
        if($param["type"]==1 && empty($data))$weight=$data["weight"];
        if(!empty($data)){
            $weight=$data["weight"];
            $update_data[]=[
                "weight"=>$dataInfo["weight"],
                "confirmation_category_id"=>$data["confirmation_category_id"]
            ];
        }
        $update_data[]=[
            "weight"=>$weight,
            "confirmation_category_id"=>$param["confirmation_category_id"]
        ];
        if(empty($update_data)) {
            $this->error = '失败';
            return false;
        }
        if ($this->allowField(true)->saveAll($update_data)) {
            //修改记录
            //updateActionLog($user_id, 'crm_customer', $confirmation_id, $dataInfo->data, $param);
            $data = [];
            $data['confirmation_id'] = $param["confirmation_id"];
            return $data;
        } else {
            $this->error = '失败';
            return false;
        }
    }
}